var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modal: false,
    productList: [], // 拼团商品列表
    group: {}, // 拼团详情
    groupList: {},
    groupId: '',
    //1-拼团中 2-拼团成功 3-拼团失败
    status: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      status: options.status || 1,
      groupId: options.groupId
    })
    that.groupDetail();
    that.groupData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  groupData() {
    let that = this;
    util.request('goods/list', {
      type: 'IS_GROUP'
    }).then(function (res) {
      if (res.code === 0) {
        that.setData({
          productList: res.data.records
        })
      }
    });
  },
  toGroupList() {
    wx.navigateTo({
      url: '/pages/groupList/groupList'
    })
  },
  hideModal(){
    this.setData({
      modal: false
    })
  },
  groupDetail() {
    let that = this;
    util.request('group/detail', {
      groupId: that.data.groupId
    }).then(function (res) {
      if (res.code === 0) {
        that.setData({
          groupList: res.list,
          group: res.data
        })

        let EndTime = that.data.group.expireTime || [];
        let NowTime = new Date().getTime();
        let totalSecond = Date.parse(EndTime.replace(/-/g, '/')) - NowTime || [];

        that.data.group.totalSecond = totalSecond / 1000

        that.setData({
          group: res.data
        })
      }
    });
  },
  showModal() {
    this.setData({
      modal: true
    })
  },
  goodsDetail: function (event) {
    let goodsId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/groupDetail/groupDetail?id=' + goodsId
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.groupDetail();
    this.groupData();
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.groupDetail();
    this.groupData();
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    let shareDesc = '超值拼团：' + that.data.group.goodsName;
    shareDesc += ' ' + that.data.group.price + "元";
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: shareDesc, // 默认是小程序的名称(可以写slogan等)
      path: '/pages/groupDetail/groupDetail?id=' + that.data.group.goodsId,
      imageUrl: that.data.group.listPicUrl,
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          util.showMsg('转发成功');
        }
      },
      fail: function (res) {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: function () {
        // 转发结束之后的回调（转发成不成功都会执行）
      }
    };
    return shareObj;
  }
})
